﻿using System;
using System.Collections.Generic;
using System.Text;


using Gx.Model.Role;
using Gx.Dal.Role;
using Gx.Common;
using Gx.Model;
using Gx.Model.Enums;
using System.Web;

namespace Gx.Bll.Role
{
    /// <summary>
    /// 
    /// </summary>
    public class M_UserInfoBll
    {
        /// <summary>
        /// 根据登录账号和密码判断是否存在该账号
        /// </summary>
        /// <param name="userLogin">登录账号</param>
        /// <param name="userPwd">登录密码</param>
        /// <param name="result">登陆状态</param>
        /// <returns></returns>
        public ReturnModel<string> UserLogin(string userLogin, string userPwd)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                ReturnModel<string> r = new ReturnModel<string>();
                string md5Pwd = Base64Helper.Encryption(userPwd, ConfigHelper.CookieSecretKey);
                M_UserInfo m = dal.UserLogin(userLogin, md5Pwd);
                if (m == null)
                {
                    r.Code = (int)Code.操作失败;
                    r.Msg = "账户或密码不存在!";
                    return r;
                }
                CookieHelper.RemoveCookie(LoginHelper.UserCookie);
                List<string> key = new List<string>
                {
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEUSERID),
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEUSERNAME),
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEROLEID)
                };
                List<object> value = new List<object>
                {
                    LoginHelper.GetCookieEncryption(m.UserId.ToString()),
                    LoginHelper.GetCookieEncryption(m.UserName),
                    LoginHelper.GetCookieEncryption(m.RoleId.ToString())
                };
                CookieHelper.SetCookieValues(LoginHelper.UserCookie, key.ToArray(), value.ToArray(), DateTime.Now.AddHours(8));
                r.Code = (int)Code.操作成功;
                r.Msg = "登录成功!";
                r.Data = "/Home/Index";
                return r;
            }
        }

        /// <summary>
        /// 获取用户数据列表
        /// </summary>
        /// <param name="userName">条件</param>
        /// <param name="userLogin">条件</param>
        /// <param name="pageIndex">数据条数</param>
        /// <param name="pageSize">类型</param>
        /// <param name="totalCount">总计数</param>
        /// <returns>DataTable</returns>
        public List<M_UserInfo> GetUseInfoList(string userName, string userLogin, int pageIndex, int pageSize, out int totalCount)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;

                StringBuilder strWhere = new StringBuilder();
                if (!userName.IsNullOrEmpty())
                {
                    strWhere.AppendFormat(" AND UserName='{0}'", userName);
                }
                if (!userLogin.IsNullOrEmpty())
                {
                    strWhere.AppendFormat(" AND UserLogin='{0}'", userLogin);
                }
                return dal.GetUserInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
            }

        }

        /// <summary>
        /// 根据账号旧密码去更新密码
        /// </summary>
        /// <param name="UserLogin">登录账号</param>
        /// <param name="NewsPwd">新密码</param>
        /// <returns></returns>
        public ReturnModel<bool> EditPassWord(int id, string userPwd)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败"
                };
                string newPwd = Base64Helper.Encryption(userPwd, ConfigHelper.CookieSecretKey);
                bool res = dal.EditPassWord(id, newPwd);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        public ReturnModel<bool> EditPassWord(string userPwd)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败"
                };
                if (string.IsNullOrEmpty(userPwd))
                {
                    return r;
                }
                if (userPwd.Length < 6)
                {
                    r.Msg = "密码长度不得低于5位";
                    return r;
                }
                string newPwd = Base64Helper.Encryption(userPwd, ConfigHelper.CookieSecretKey);
                bool res = dal.EditPassWord(LoginHelper.GetUserId(), newPwd);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "密码修改成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 新增一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnModel<bool> AddUserInfo(M_UserInfo model)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败"
                };
                if (IsExistsUserInfo(model.UserLogin, model.UserId) > 0)
                {
                    r.Code = (int)Code.重复数据;
                    r.Msg = "登录账户重复,请重新输入";
                    return r;
                }
                model.UserPwd = Base64Helper.Encryption(model.UserPwd, ConfigHelper.CookieSecretKey);
                if (dal.AddUserInfo(model))
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 编辑一条用户数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnModel<bool> EditUserInfo(M_UserInfo model)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败"
                };
                model.LastEditUser = LoginHelper.GetUserId();
                model.LastEditTime = DateTime.Now;
                bool res = dal.EditUserInfo(model);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="OptionType"></param>
        /// <returns></returns>
        public ReturnModel<bool> DelUserInfo(int id)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败"
                };
                bool res = dal.DelUserInfo(id, LoginHelper.GetUserId(), DateTime.Now);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 是否存在用户，不论是否删除
        /// </summary>
        /// <param name="userLogin"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int IsExistsUserInfo(string userLogin, int userId)
        {
            using (M_UserInfoDal dal = new M_UserInfoDal())
            {
                return dal.IsExistsUserInfo(userLogin, userId);
            }
        }
    }
}
